package com.igeek.collateral.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.igeek.collateral.entity.Inbound;
import com.igeek.collateral.entity.Outbound;
import com.igeek.collateral.mapper.InboundMapper;
import com.igeek.collateral.mapper.OutboundMapper;
import com.igeek.collateral.vo.InOutNumberVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

/**
 * TODO
 *
 * @author chh
 * @since 2024/7/17
 **/
@Slf4j
@Component
//开启定时任务
@EnableScheduling
//开启异步执行
//@EnableAsync
public class UpdataCount {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private InboundMapper inboundMapper;

    @Autowired
    private OutboundMapper outboundMapper;

    //押品的总记录数（包括每天对应的出入库的数量）
    public static final String AllNum="AllNum:";


    //每天定时更新出库和入库记录数
    @Scheduled(cron = "0 0 0 17 * ?")
    public void UpdateNum()
    {
        List<Map<String, InOutNumberVo>> maps = new ArrayList<>();

        //上个月 1-31
        Date date = DateUtil.lastMonth();
        Date beginOfMonth = DateUtil.beginOfMonth(date);
        Date endOfMonth = DateUtil.endOfMonth(date);

        for (Date d=beginOfMonth; d.before(endOfMonth);d=DateUtil.offsetDay(d , 1)) {

            //先从数据库中读取进库数,出库数
            Integer inNum = inboundMapper.selectList(new LambdaQueryWrapper<Inbound>()
                            .eq(Inbound::getDate, d)
                            .eq(Inbound::getStatus, 1))
                    .size();
            Integer outNum = outboundMapper.selectList(new LambdaQueryWrapper<Outbound>()
                            .eq(Outbound::getDate, d)
                            .eq(Outbound::getStatus, 1))
                    .size();
            //先封装InOutNumberVo
            InOutNumberVo in = InOutNumberVo.builder()
                    .inNumber(inNum)
                    .outNumber(outNum)
                    .build();
            //封装Map
            Map<String, InOutNumberVo> map = new HashMap<>();
            //将时间转换为字符串
            String f = DateUtil.format(d, "yyyy-MM-dd");
            map.put(f, in);
            //最后总记录数中加入
            maps.add(map);
        }

        /*//上个月
        LocalDate localDate = LocalDate.now().minusMonths(1);


        while (localDate.plusDays(1).isBefore(LocalDate.now())) {
            log.info("时间为{}", localDate);

            //(LocalDate与Date之间的转换)
            *//*ZoneId zoneId = ZoneId.systemDefault();
            ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
            Date time = Date.from(zdt.toInstant());*//*

            //将date类型的数据转成我们需要的样子
//            if(!ObjectUtils.isEmpty(time))
//            {
//                DateUnit.
//            }

            //先从数据库中读取进库数,出库数
            Integer inNum = inboundMapper.selectList(new LambdaQueryWrapper<Inbound>()
                            .eq(Inbound::getDate, time)
                            .eq(Inbound::getStatus, 1))
                    .size();
            Integer outNum = outboundMapper.selectList(new LambdaQueryWrapper<Outbound>()
                            .eq(Outbound::getDate, time)
                            .eq(Outbound::getStatus, 1))
                    .size();
            //先封装InOutNumberVo
            InOutNumberVo in = InOutNumberVo.builder()
                    .inNumber(inNum)
                    .outNumber(outNum)
                    .build();
            //封装Map
            Map<Date, InOutNumberVo> map = new HashMap<>();
            map.put(time, in);
            //最后总记录数中加入
            maps.add(map);
            localDate = localDate.plusDays(1);
        }*/

    //最后在redis中加入最新信息
        log.info("---------发送成功---------------");
        redisTemplate.opsForValue().set(AllNum,maps);
    }

}
